'use strict'

const Service = require('egg').Service

class HomeService extends Service {
  async user() {
    const { ctx, app } = this
    console.log(app.mysql.query, 1111111111)
    const QUERY_STR = 'id, name'
    let sql = `select ${QUERY_STR} from list`
    try {
      const result = await app.mysql.query(sql)
      return result
    } catch (error) {
      return null
    }
  }

  async addUser(name) {
    const { ctx, app } = this
    console.log(
      { name }
    );
    try {
      const result = await app.mysql.insert('list', { name })
      return result
    } catch (error) {
      return null
    }
  }

  async editUser(id, name) {
    const { ctx, app } = this
    try {
      const result = await app.mysql.update('list', {name}, {
        where: {
          id
        }
      })
      return result
    } catch (error) {
      return error
    }
  }

  async deleteUser(id) {
    const { ctx, app } = this
    try {
      const result = await app.mysql.delete('list', {id})
      return result
    } catch (error) {
      return error
    }
  }
}
module.exports = HomeService